<template>
  <div class="finance-list">
    <el-row :gutter="10" justify="end">
      <el-col :span="3.5"><el-input placeholder="输入跟单人名字查询" v-model.trim="user_name" /></el-col>
      <el-col :span="3.5"><el-input placeholder="输入供应商名字查询" v-model.trim="supplier_name" /></el-col>
      <el-col :span="1"><el-button type="primary" @click="handleSearch">查询</el-button></el-col>
      <el-col :span="1"><el-button type="primary" @click="addfinance = true">新建</el-button></el-col>
    </el-row>
  </div>

  <el-divider />

    <el-table :data="financeList" style="width: 100%" border :header-cell-style="{background:'rgb(250,250,250)'}">
      <el-table-column prop="supplier_name" label="供应商名字" width="200" />
      <el-table-column prop="supplier_contact" label="联系人" width="200" />
      <el-table-column prop="user_name" label="跟单人" width="200" />
      <el-table-column prop="order_id" label="订单号" width="300" />
      <el-table-column prop="paid" label="已付" width="200" />
      <el-table-column prop="payment" label="待付" width="200" />
      <el-table-column prop="remark" label="备注" width="300" />
      <el-table-column label="操作" width="150" ><a href="./detail">详情</a></el-table-column>
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
      
    </el-table>
    <el-pagination
    background
    layout="prev, pager, next"
    :total="total"
    :page-size="sizes"
    @current-change="handlePage"
    />
  <div>
    <el-dialog
    v-model="addfinance"
    title="新增财务对账单"
    >
      <el-form>
        <el-form-item>
          <el-radio-group v-model="radio1" class="ml-4">
            <el-radio label="1" size="large">入库对账单</el-radio>
            <el-radio label="2" size="large">退货对账单</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <el-form :rules="rules" :inline="true">
        <el-form-item label="对账单号：" prop="order">
          <el-input style="width: 270px">
            <template #append>
              <el-button
                style="color: white; background: #409eff"
                :icon="Search"
              ></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="金额：">
          <el-input style="width: 290px"></el-input>
        </el-form-item>
        <el-form-item label="已付：">
          <el-input style="width: 290px"></el-input>
        </el-form-item>
        <el-form-item label="待付：">
          <el-input style="width: 290px"></el-input>
        </el-form-item>
        <el-form-item label="跟单人：">
          <el-input style="width: 290px"></el-input>
        </el-form-item>
        <el-form-item label="供应商：">
          <el-input style="width: 290px" placeholder="自动带入"></el-input>
        </el-form-item>
        <el-form-item label="联系人：">
          <el-input style="width: 290px" placeholder="自动带入"></el-input>
        </el-form-item>
      </el-form>
      <el-form :rules="rules">
        <el-form-item label="备注：" prop="order">
          <el-input style="width: 100%" placeholder="输入备注信息"></el-input>
        </el-form-item>
        <el-form-item label="物资信息：">
          <el-table :data="addData" style="width: 100%" border :header-cell-style="{background:'rgb(250,250,250)'}">
            <el-table-column prop="encoded" label="编码" width="210" />
            <el-table-column prop="name" label="物资名字" width="210" />
            <el-table-column prop="place" label="产地" width="210" />
            <el-table-column prop="price" label="单价" width="210" />
          
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
          </el-table>
        </el-form-item>
      </el-form>
      <el-row :gutter="30" justify="end">
        <el-col :span="2"
          ><el-button @click="addfinance = false">取 消</el-button></el-col
        >
        <el-col :span="2"><el-button type="primary">确 定</el-button></el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { Search } from "@element-plus/icons-vue"
import { addFinanceApi,approveFinanceApi,getFinanceApi } from "@/apis/Purchase/finance"

export default {
  setup() {
    return {
      Search,
    };
  },
  data() {
    return {
      user_name: '',
      supplier_name: '',
      total: 0,
      pages: 1,
      sizes: 10,
      addfinance: false,
      radio1: "1",
      rules: {
        order: [
          {
            required: true,
          },
        ],
      },
      financeList: [],
      
    }
  },
  created() {
    this.handleGet()
  },
  methods: {
    async handleGet(user_name, supplier_name, size = this.sizes, page = this.pages) {
      try {
        const res = await getFinanceApi({user_name, supplier_name, size, page}) 
        if(res.code === 200) {
          this.financeList = res.data.rows
          this.total = res.data.count
        }
      } catch (error) {
        console.log(error);
      }
    },

    // 搜索功能
    handleSearch() {
      this.financeList.forEach(item => {
          if(item.user_name.includes(this.user_name)) {
            this.handleGet(this.user_name, this.supplier_name)
          }
          if(item.supplier_name.includes(this.supplier_name)) {
            this.handleGet(this.user_name, this.supplier_name)
        }
      })
    },

    // 分页器切换
    handlePage(e) {
      this.pages = e
      this.handleGet()
    }
    
  },
  setup() {
    return {
      Search,
    }
  }
}

</script>

<style lang="less">
a {
  text-decoration: none;
}

.el-pagination {
  width: 300px;
  position: absolute;
  left: 45%;
  margin-top: 30px;
}

.el-dialog > .el-dialog__body {
  border-top: 1px solid #e9e9e9;
  border-bottom: 1px solid #e9e9e9;
}

.el-table--border,
.el-table--group {
  border: 1px solid rgb(236, 236, 236);
}
</style>
